package product;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.text.NumberFormat;
import java.util.ArrayList;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.TableOrder;

import com.orsoncharts.legend.StandardLegendBuilder;

/**
 * A demo showing four pie charts.
 */
public class MultiPieChart extends ApplicationFrame {

    private String[][] codeProductSale;
	private String[] codeList;
	private ArrayList totalProductList;
	private ArrayList totalSaleList;

	public MultiPieChart(String[][] codeProductSale) {
        super("선택된 제품별 파이 그래프(생산, 판매, 재고량)");

        this.codeProductSale = codeProductSale;
        
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart, true, true, true, false, true);
        chartPanel.setPreferredSize(new Dimension(800, 600));
        setContentPane(chartPanel);
        
    }
        
    public MultiPieChart(String[] codeList, ArrayList totalProductList) {
        super("선택된 제품별 파이 그래프(날짜별 재고량)");
        this.codeList = codeList;
        this.totalProductList = totalProductList;
        this.totalSaleList = totalSaleList;
        
        final CategoryDataset dataset = createDataset2();
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart, true, true, true, false, true);
        chartPanel.setPreferredSize(new Dimension(800, 600));
        setContentPane(chartPanel);
	}

	private CategoryDataset createDataset2() {
    	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		String[][] ext;
		String[][] ext2;
		for (int j = 0; j < totalProductList.size(); j++) {
			ext = (String[][]) totalProductList.get(j);
			for (int j2 = 0; j2 < ext.length; j2++) {
				if (ext[j2][0]==null) continue;
				if (j >= codeList.length) { 
				dataset.addValue(Integer.parseInt((String)ext[j2][1]), ext[j2][0], codeList[j-codeList.length]); // 조각 , 파이 차트
					System.out.println(Integer.parseInt((String)ext[j2][1]) +" || "+ ext[j2][0] +" || "+ codeList[j-codeList.length]);
				}
				else {
					dataset.addValue(Integer.parseInt((String)ext[j2][1]), ext[j2][0], codeList[j]); // 조각 , 파이 차트
					System.out.println(Integer.parseInt((String)ext[j2][1]) +" || "+ ext[j2][0] +" || "+ codeList[j]);
				}
			}
		}
		return dataset;
	}

	private CategoryDataset createDataset() {
    	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    	String codeName;
    	for (int i = 0; i < codeProductSale.length; i++) {
				if (codeProductSale[i][2]==null) codeProductSale[i][2]=0+"";
				codeName = codeProductSale[i][0];
				System.out.println(codeName);
				dataset.addValue(Integer.parseInt(codeProductSale[i][2]+""), "판매", codeName + " : Total Product " + codeProductSale[i][1]); // 조각 , 파이 차트
		    	dataset.addValue(Integer.parseInt(codeProductSale[i][1]) - Integer.parseInt(codeProductSale[i][2]), "재고", codeName + " : Total Product " + codeProductSale[i][1]); // 조각 , 파이 차트
    	}
    	return dataset;
    }

	
	
    private JFreeChart createChart(final CategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createMultiplePieChart3D(
            "", dataset, TableOrder.BY_COLUMN, false, true, false //by_row도 가능할 듯
        ); 
        chart.setBackgroundPaint(new Color(216, 255, 216));
        final MultiplePiePlot plot = (MultiplePiePlot) chart.getPlot();
        final JFreeChart subchart = plot.getPieChart();
        plot.setLimit(0.01);
        final PiePlot p = (PiePlot) subchart.getPlot();
        p.setLabelGenerator(new StandardPieSectionLabelGenerator(
                "{0} : {2}({1})", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
            ));
        p.setLabelFont(new Font("SansSerif", Font.PLAIN, 15));
        p.setInteriorGap(0.12);
        p.setForegroundAlpha(0.75f); // 색의 진하기

        return chart;
    }
}
